package Morsulus::Actions;

use warnings;
use strict;
use Carp;

use version; our $VERSION = qv('0.0.2');
use Class::Std;
use Carp;
use Daud;
use Readonly;

{
    my %raw_form_of : ATTR;
    my %cooked_form_of : ATTR;
    my %source_of : ATTR;
    my %name_of : ATTR;
    my %armory_of : ATTR;
    my %quoted_names_of : ATTR;
    my %second_name_of : ATTR;
    my %notes_of : ATTR;

    Readonly my $SEPARATOR => q{|};
    Readonly my $EMPTY_STR => q{};
    Readonly my $PERIOD    => q{.};
    Readonly my $NEWLINE => qq{\n};

    sub as_str : STRINGIFY {
        my ( $self, $ident ) = @_;
        return sprintf '%s', join $SEPARATOR, $raw_form_of{$ident},
            $cooked_form_of{$ident}, $source_of{$ident}, $name_of{$ident},
            $armory_of{$ident}, $second_name_of{$ident},
            @{ $quoted_names_of{$ident} };
    }

    sub BUILD {
        my ( $self, $ident, $arg_ref ) = @_;

        # suck in values, defaults
        $raw_form_of{$ident}    = Daud::daudify( $arg_ref->{action} );
        $source_of{$ident}      = $arg_ref->{source};
        $name_of{$ident}        = Daud::daudify( $arg_ref->{name} );
        $armory_of{$ident}      = Daud::daudify( $arg_ref->{armory} );
        $second_name_of{$ident} = exists $arg_ref->{name2}
            ? Daud::daudify( $arg_ref->{name2} )
            : $EMPTY_STR;
        $quoted_names_of{$ident} = [];
        $notes_of{$ident}        = $EMPTY_STR;

        # clean up raw action
        $cooked_form_of{$ident} = $raw_form_of{$ident};
        $cooked_form_of{$ident} =~ s{[ ][(]see[^)]+[)]\z}{}xsmi;
        if ( $cooked_form_of{$ident}
            =~ m{\A([^(]+)[ ]([(]important[ ]non-sca[ ].+[)])\z}ixsm )
        {
            $cooked_form_of{$ident} = $1;
            $notes_of{$ident}       = $2;
        }
        $cooked_form_of{$ident} =~ s{\s+\z}{}xsm;

        $self->extract_quoted_names();
        $self->normalize_cooked_action();
        $self->normalize_armory();
    }

    sub normalize_armory {
        my ($self) = @_;
        my $ident = ident($self);
        if ( $armory_of{$ident} ) {
            $armory_of{$ident} .= $PERIOD;
        }
    }

    sub extract_quoted_names {
        my ($self) = @_;
        my $ident = ident($self);
        $quoted_names_of{$ident}
            = [ $cooked_form_of{$ident} =~ m{ " ([^"]+) " }gxsm ];
    }

    sub normalize_cooked_action {
        my ($self) = @_;
        my $ident = ident($self);
        $cooked_form_of{$ident} =~ s{"[^"]+"}{"x"}gxsm;
        $cooked_form_of{$ident} = lc $cooked_form_of{$ident};
        $cooked_form_of{$ident} =~ s{acceptance\ of\ transfer\ of\ (.+)\ from\ "x"}{$1}xsm;
    }

    sub permute {
        my ( $self, $string ) = @_;
        return $string if $string =~ /,/;

        $string =~ s/^the //i;
        if ( $string
            =~ /^(award|barony|braithrean|brotherhood|canton|casa|chateau|clann?|companionate|company|crown principality|domus|dun|fellowship|freehold|guild|honou?r of the|house|household|hous|ha?us|h\{u'\}sa|keep|kingdom|league|l'ordre|maison|orde[nr]|ord[eo]|ordre|principality|province|riding|shire) (.*)/i
            )
        {
            $string = "$2, $1";
            if ( $string
                =~ /^(af|an|aus|d[eou]|de[ils]|dell[ao]|in|na|of?|van|vo[mn]) (.*)/i
                )
            {
                $string = "$2 $1";
            }
            if ( $string =~ /^(das|de[mn]?|der|die|el|l[ae]|les|the) (.*)/i )
            {
                $string = "$2 $1";
            }
        }
        return $string;
    }
    
    my %transforms = (
        'name' => { 'name' => [ 'N' ], },
        'branch name' => { 'name' => [ 'BN' ], },
        'alternate name "x"' => { 'name_for' => [ 'AN' ] },
        'alternate name "x" and badge' => { 'name_for' => [ 'AN' ],
            'badge_for' => [] },
        'exchange of primary and alternate name "x"' => { 'name_change' => [ 'NC' ],
            'name_for' => [ 'AN' ],
            'owned_name_release' => [ 'AN', 'converted to primary name' ] },
        'badge for "x"' => { 'badge_for' => [] },
        'badge for the "x"' => { 'badge_for' => [ "the " ] },
        'reblazon of badge for "x"' => { 'badge_for' => [] },
        'reblazon of badge for the "x"' => { 'badge_for' => [ "the " ] },
        'name and device' => { 'name' => [ 'N' ], 'armory' => [ 'd'], },
        'branch name and device' => { 'name' => [ 'BN' ], 'armory' => [ 'd'], },
        'name and badge' => { 'name' => [ 'N' ], 'armory' => [ 'b'], },
        'household name "x" and badge' => { 'name_owned_by' => [ 'HN' ], 
            'badge_for' => [], },
        'household name "x" and badge change' => { 'name_owned_by' => [ 'HN' ], 
            'badge_for' => [], },
        'household name "x"' => { 'name_owned_by' => [ 'HN' ], },
        'device' => { 'armory' => [ 'd' ], },
        'augmentation' => { 'armory' => [ 'a' ], },
        'device (important non-sca armory)' => { 'armory' => [ 'd', 'Important Non-SCA armory' ], },
        'badge' => { 'armory' => [ 'b' ], },
        'name change from "x"' => { 'name_change' => [ 'NC' ], },
        'order name change from "x" to "x"' => { 'order_name_change' => [ 'OC' ], },
        'branch name change from "x"' => { 'name_change' => [ 'BNC' ], },
        'designator change from "x"' => { 'designator_change' => [ 'u' ], },
        'designator change from "x" and device change' => { 'designator_change' => [ 'u' ],
            'armory' => [ 'd' ], },
        'name change from "x" and device change' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'd' ], },
        'name change from "x" and device' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'd' ], },
        'name change from "x" and badge' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'b' ], },
        'name reconsideration from "x" and device' => { 'name_correction' => [ 'Nc' ],
            'armory' => [ 'd' ], },
        'name reconsideration from "x"' => { 'name_correction' => [ 'Nc' ], },
        'name correction from "x"' => { 'name_correction' => [ 'Nc' ], },
        'name correction from "x" and device' => { 'name_correction' => [ 'Nc' ],
            'armory' => [ 'd' ], },
        'name change from holding name "x"' => { 'name_change' => [ 'NC' ], },
        'name change from holding name "x" and badge' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'b' ], },
        'name change from holding name "x" and device' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'd' ], },
        'name change from holding name "x" and device change' => { 'name_change' => [ 'NC' ], 
            'armory' => [ 'd' ], },
        'order name "x"' => { 'name_owned_by' => [ 'O' ] },
        'order name "x" and badge' => { 'name_owned_by' => [ 'O' ],
            'armory' => [ 'b' ], },
        'award name "x"' => { 'name_owned_by' => [ 'O' ] },
        'award name "x" and badge' => { 'name_owned_by' => [ 'O' ],
            'armory' => [ 'b' ], },
        'heraldic title "x"' => { 'name_owned_by' => [ 't' ] },
        'device change' => { 'armory' => [ 'd' ], },
        'badge change' => { 'armory' => [ 'b' ], },
        'change of badge to device' => { 'armory_release' => [ 'b', 'converted to device' ],
            'armory' => [ 'd' ], },
        'change of device to badge' => { 'armory_release' => [ 'd', 'converted to badge' ],
            'armory' => [ 'b' ], },
        'reblazon of device' => { 'armory' => [ 'd' ], },
        'reblazon of augmentation' => { 'armory' => [ 'a' ], },
        'reblazon of badge' => { 'armory' => [ 'b' ], },
        'joint badge with "x"' => { 'joint' => [],
            'joint_badge' => [] },
        'joint badge' => { 'normalize_joint_badge' => [] },
        'joint household name "x"' => { 'normalize_joint_household_name' => [] },
        'device changed/retained' => { 'armory_release' => [ 'd', 'changed/retained' ],
            'armory' => [ 'd' ], },
        'device changed/retained as ancient arms' => { 'armory_release' => [ 'd', 'changed/retained as Ancient Arms' ],
            'armory' => [ 'b' , 'Ancient Arms' ], },
        'device changed/released' => { 'armory_release' => [ 'd', 'changed/released' ] },
        'badge changed/released' => { 'armory_release' => [ 'b', 'changed/released' ] },
        'device reblazoned' => { 'armory_release' => [ 'd', 'reblazoned' ] },
        'badge reblazoned' => { 'armory_release' => [ 'b', 'reblazoned' ] },
        'augmentation reblazoned' => { 'armory_release' => [ 'a', 'reblazoned' ] },
        'device released' => { 'armory_release' => [ 'd', 'released' ] },
        'release of name and device' => { 'name_release' => [ 'N', 'released' ],
            'armory_release' => [ 'd', 'released' ] },
        'release of branch name' => { 'name_release' => [ 'BN', 'released' ], },
        'release of branch name and device' => { 'name_release' => [ 'BN', 'released' ],
            'armory_release' => [ 'd', 'released' ] },
        'release of household name "x" and badge' => { 'owned_name_release' => [ 'HN', 'released' ],
            'armory_release' => [ 'b', 'released' ] },
        'release of device' => { 'armory_release' => [ 'd', 'released' ] },
        'release of badge' => { 'armory_release' => [ 'b', 'released' ] },
        'badge release' => { 'armory_release' => [ 'b', 'released' ] },
        'release of badge for the "x"' => { 'armory_release' => [ 'b', 'released' ] },
        'release of heraldic title "x"' => { 'owned_name_release' => [ 't', 'released' ] },
        'release of heraldic title' => { 'name_release' => [ 't', 'released' ] },
        'release of alternate name "x"' => { 'owned_name_release' => [ 'AN', 'released' ] },
        'holding name and device' => { 'holding_name' => [],
            'armory' => [ 'd' ] },
        'transfer of heraldic title "x" to "x"' => {  'transfer_name' => [ 't', ], },
        'transfer of order name "x" to "x"' => {  'transfer_name' => [ 'O', ], },
        'transfer of device to "x"' => {  'transfer_armory' => [ 'd', ], },
        'transfer of badge to "x"' => {  'transfer_armory' => [ 'b', ], },
        #'transfer of household name "x" to "x"' => {  'transfer' => [ 'HN', ], },
        'blanket permission to conflict with name and device' => 
            { 'blanket_permission_name' => [ 'BP', 'name' ], 
            'blanket_permission_armory' => [ 'BP', 'device' ], },
        'blanket permission to conflict with name' => 
            { 'blanket_permission_name' => [ 'BP', 'name' ],  },
        'blanket permission to conflict with badge' => 
            { 'blanket_permission_armory' => [ 'BP', 'badge' ], },
        'blanket permission to conflict with device' => 
            { 'blanket_permission_armory' => [ 'BP', 'device' ], },
        'blanket permission to conflict with alternate name "x"' => 
            { 'blanket_permission_secondary_name' => [ 'BP', 'alternate name' ], },
        );
    
    sub make_db_entries
    {
        my ($self) = shift;
        my $ident = ident($self);
        if (not exists $transforms{$cooked_form_of{$ident}})
        {
            carp "Unknown action in $self";
            return;
        }
        
        my @entries;
        foreach my $act_sub (keys %{$transforms{$cooked_form_of{$ident}}})
        {
            my @action_results = $self->$act_sub(@{$transforms{$cooked_form_of{$ident}}->{$act_sub}});
            foreach my $action_result (@action_results)
            {
            push @entries, join($SEPARATOR, @$action_result);
            }
        }
        return join($NEWLINE, @entries, '');
    }
    
    sub name_for
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, "For $name_of{$ident}", 
            $notes_of{$ident} ];
    }
    
    sub name_change
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, "See $name_of{$ident}",
            $notes_of{$ident} ];
    }
    
    sub designator_change
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, $name_of{$ident},
            $notes_of{$ident} ];
    }
    
    sub order_name_change
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, $quoted_names_of{$ident}->[1],
            $notes_of{$ident} ];
    }
    
    sub name_correction
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, $name_of{$ident},
            $notes_of{$ident} ];
    }
    
    sub name_owned_by
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, $type, $self->quote_joint($name_of{$ident}),
            $notes_of{$ident} ];
    }
    
    sub quote_joint
    {
        my ($self, $name) = @_;
        my @parts = split(/ and /, $name);
        if (@parts == 1)
        {
            return $name;
        }
        else
        {
            $notes_of{ident($self)} .= '(FIXME: add j record)';
            return join(" and ", map { "\"$_\"" } @parts);
        }
    }
    
    sub name
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        $type ||= 'N';
        return [ $name_of{$ident}, $source_of{$ident}, $type, $EMPTY_STR, 
            $notes_of{$ident}];
    }
    
    sub holding_name
    {
         my ($self) = @_;
         $notes_of{ident($self)} .= "(Holding name)";
         return $self->name();
    }
    
    sub joint
    {
        my ($self) = @_;
        my $ident = ident($self);
        return [ $quoted_names_of{$ident}->[0], $source_of{$ident}, 'j',
            $name_of{$ident}, $notes_of{$ident} ];
    }
    
    sub badge_for
    {
        my ($self, $article) = @_;
        my $ident = ident($self);
        $article ||= $EMPTY_STR;
        return [ $name_of{$ident}, $source_of{$ident}, 'b', $armory_of{$ident},
            "$notes_of{$ident}(For $article$quoted_names_of{$ident}->[0])" ];
    }
    
    sub normalize_joint_badge
    {
        my ($self) = @_;
        my $ident = ident($self);
        my @names = split(/ and /, $name_of{$ident});
        $name_of{$ident} = $names[0];
        $quoted_names_of{$ident}->[0] = $names[1];
        return ($self->joint_badge(), $self->joint());
    }
    
    sub normalize_joint_household_name
    {
        my ($self) = @_;
        my $ident = ident($self);
        my @names = split(/ and /, $name_of{$ident});
        return ([ $self->permute($quoted_names_of{$ident}->[0]),
            $source_of{$ident}, 'HN', join(" and ", map { "\"$_\"" } @names),
            $notes_of{$ident} ], 
            [$names[1], $source_of{$ident}, 'j', $names[0], $notes_of{$ident} ]);
    }
    
    sub joint_badge
    {
        my ($self) = @_;
        my $ident = ident($self);
        local $notes_of{$ident} .= "(JB: $quoted_names_of{$ident}->[0])";
        return $self->armory('b');
    }
    
    sub armory
    {
        my ($self, $type, $note) = @_;
        my $ident = ident($self);
        $note = "($note)" if $note;
        $note ||= '';
        return [ $name_of{$ident}, $source_of{$ident}, $type, $armory_of{$ident},
            $notes_of{$ident}.$note ];
    }
    
    sub armory_release
    {
        my ($self, $type, $reason) = @_;
        my $ident = ident($self);
        return [ $name_of{$ident}, "-$source_of{$ident}", $type, 
            $armory_of{$ident}, "$notes_of{$ident}(-$reason)" ];
    }
    
    sub name_release
    {
        my ($self, $type, $reason) = @_;
        my $ident = ident($self);
        return [ $name_of{$ident}, "-$source_of{$ident}", $type, $EMPTY_STR,
            "$notes_of{$ident}(-$reason)" ];
    }
        
    sub owned_name_release
    {
        my ($self, $type, $reason) = @_;
        my $ident = ident($self);
        my $for = $type eq 'AN' ? 'For ' : '';
        return [ $quoted_names_of{$ident}->[0], "-$source_of{$ident}", $type, $for.$name_of{$ident},
            "$notes_of{$ident}(-$reason)" ];
    }
        
    sub transfer_armory
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $name_of{$ident}, "-$source_of{$ident}", $type, 
            $armory_of{$ident}, "$notes_of{$ident}(-transferred to $quoted_names_of{$ident}->[-1])" ];
    }
    
    sub transfer_name
    {
        my ($self, $type) = @_;
        my $ident = ident($self);
        return [ $quoted_names_of{$ident}->[0], "-$source_of{$ident}", $type, 
            $name_of{$ident}, "$notes_of{$ident}(-transferred to $quoted_names_of{$ident}->[-1])" ];
    }
    
    sub blanket_permission_name
    {
        my ($self, $type, $item_type) = @_;
        my $ident = ident($self);
        $type ||= 'N';
        return [ $name_of{$ident}, $source_of{$ident}, $type, $EMPTY_STR, 
            "$notes_of{$ident}(Blanket permission to conflict with $item_type granted $source_of{$ident})"];
    }
    
    sub blanket_permission_secondary_name
    {
        my ($self, $type, $item_type) = @_;
        my $ident = ident($self);
        $type ||= 'N';
        return [ $quoted_names_of{$ident}->[0], $source_of{$ident}, $type, $name_of{$ident}, 
            "$notes_of{$ident}(Blanket permission to conflict with $item_type granted $source_of{$ident})"];
    }
    
    sub blanket_permission_armory
    {
        my ($self, $type, $item_type) = @_;
        my $ident = ident($self);
        $type ||= 'd';
        return [ $name_of{$ident}, $source_of{$ident}, $type, $armory_of{$ident}, 
            "$notes_of{$ident}(Blanket permission to conflict with $item_type granted $source_of{$ident})"];
    }
    
}

1; # Magic true value required at end of module
__END__

=head1 NAME

Morsulus::Actions - Convert actions extracted from the XML into useful forms.


=head1 VERSION

This documentation refers to Morsulus::Actions version 0.0.1.


=head1 SYNOPSIS

    use Morsulus::Actions;
    
    # $action_line is output from the xml2actions filter
    # $date is yymm
    my (undef, $kingdom, $action, $name, $armory, $name2) = split(/[|]/, $action_line);
    
    my $action = Morsulus::Actions->new({action => $action,
        source => "$kingdom$date", 
        name => $name,
        armory => $armory,
        name2 => $name2});
    
    # output for db file format
    print $action->make_db_entries();
    
=head1 DESCRIPTION

=for author to fill in:
    Write a full description of the module and its features here.
    Use subsections (=head2, =head3) as appropriate.


=head1 INTERFACE 

=for author to fill in:
    Write a separate section listing the public components of the modules
    interface. These normally consist of either subroutines that may be
    exported, or methods that may be called on objects belonging to the
    classes provided by the module.


=head1 DIAGNOSTICS

=for author to fill in:
    List every single error and warning message that the module can
    generate (even the ones that will "never happen"), with a full
    explanation of each problem, one or more likely causes, and any
    suggested remedies.

=over

=item C<< Error message here, perhaps with %s placeholders >>

[Description of error here]

=item C<< Another error message here >>

[Description of error here]

[Et cetera, et cetera]

=back


=head1 CONFIGURATION AND ENVIRONMENT

=for author to fill in:
    A full explanation of any configuration system(s) used by the
    module, including the names and locations of any configuration
    files, and the meaning of any environment variables or properties
    that can be set. These descriptions must also include details of any
    configuration language used.
  
Morsulus::Actions requires no configuration files or environment variables.


=head1 DEPENDENCIES

=for author to fill in:
    A list of all the other modules that this module relies upon,
    including any restrictions on versions, and an indication whether
    the module is part of the standard Perl distribution, part of the
    module's distribution, or must be installed separately. ]

None.


=head1 INCOMPATIBILITIES

=for author to fill in:
    A list of any modules that this module cannot be used in conjunction
    with. This may be due to name conflicts in the interface, or
    competition for system or program resources, or due to internal
    limitations of Perl (for example, many modules that use source code
    filters are mutually incompatible).

None reported.


=head1 BUGS AND LIMITATIONS

=for author to fill in:
    A list of known problems with the module, together with some
    indication Whether they are likely to be fixed in an upcoming
    release. Also a list of restrictions on the features the module
    does provide: data types that cannot be handled, performance issues
    and the circumstances in which they may arise, practical
    limitations on the size of data sets, special cases that are not
    (yet) handled, etc.

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-morsulus-actions@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.


=head1 AUTHOR

Michael Houghton  C<< <herveus@cpan.org> >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006, Michael Houghton C<< <herveus@cpan.org> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
